from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from . import models
from .serializer import *
from django.core.paginator import Paginator
class CateViewInfo(APIView):
    def get(self,request):
        allobj=models.Cate.objects.all()
        obj=CateSerializers(allobj,many=True)
        return Response({"code":1000,"msg":"","data":obj.data})

#添加
class CateView(APIView):
    def get(self,request):
        p=int(request.GET.get("p",1))
        allobj=models.Cate.objects.all()
        page_obj=Paginator(allobj,per_page=3)
        page=[i for i in page_obj.page_range]
        page_data=page_obj.page(p)
        previous=page_data.has_previous()
        next=page_data.has_next()
        obj=CateSerializers(page_data,many=True)
        data={
            'data':obj.data,
            'page':page,
            'previous':previous,
            'next':next,
        }
        return Response({"code":1000,"msg":"","data":data})

    def post(self,request):
        name=request.POST.get("name")
        aobj=models.Cate.objects.filter(name=name).first()
        if aobj:
            return Response({"code":1001,"msg":"数据存在"})
        data={
            "name":name
        }
        obj=CateSerializers(data=data)
        if obj.is_valid():
            flay=obj.save()
            if flay:
                return Response({"code":1000,"msg":"创建成功"})
            return Response({"code":1002,"msg":"创建失败"})
        print(obj.errors)
        return Response({"code":1003,"msg":"参数错误"})
    def delete(self,request):
        id = request.POST.get("id")
        author_tuple=models.Cate.objects.filter(id=id).delete()
        if author_tuple[0]:
            return Response({"code":1000,"msg":"删除成功"})
        return Response({"code":1001,"msg":"删除失败"})


class BooksView(APIView):
    def get(self,request):
        id=request.GET.get("id")
        p=int(request.GET.get("p",1))
        bookobj=models.Books.objects.filter(cate_id=id).all()
        page_obj=Paginator(bookobj,per_page=3)
        page=[i for i in page_obj.page_range]
        page_data=page_obj.page(p)
        previous=page_data.has_previous()
        next=page_data.has_next()
        obj=Booksserializers(page_data,many=True)
        data={
            'data':obj.data,
            'page':page,
            'previous':previous,
            'next':next,
        }
        return Response({"code":1000,"msg":"","data":data})
    def post(self,request):
        book_name=request.POST.get("book_name","").strip()
        img=request.FILES.get("img")
        desc=request.POST.get("desc","").strip()
        cate=request.POST.get("cate")
        price=request.POST.get("price")
        aobj=models.Books.objects.filter(book_name=book_name).first()
        if aobj:
            return Response({"code":1001,"msg":"数据存在"})
        data={
            "book_name":book_name,
            'img':img,
            'desc':desc,
            'cate':cate,
            'price':price
        }
        obj=Booksserializers(data=data)
        if obj.is_valid():
            flay=obj.save()
            if flay:
                return Response({"code":1000,"msg":"创建成功"})
            return Response({"code":1002,"msg":"创建失败"})
        print(obj.errors)
        return Response({"code":1003,"msg":"参数错误"})